package cn.edu.sjtu.se.dslab.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.component.data.EntityDiagram;
import cn.edu.sjtu.se.dslab.component.data.util.DiagramXmlParser;
import cn.edu.sjtu.se.dslab.service.IDiagramService;
import cn.edu.sjtu.se.dslab.util.Const;

@Controller
@RequestMapping(value = "/diagram")
public class DiagramController {
	@Resource(name = "diagramService")
	IDiagramService diagramService;

	@RequestMapping(value = "/json/{uri}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDiagramJsonByUri(
			@PathVariable(value = "uri") String uri,
			@RequestParam(value = "entityName", required = false) String entityName) {

		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		try {

			try {
				// entityName = URLDecoder.decode(entityName,"UTF-8");

				entityName = new String(entityName.getBytes("ISO8859_1"),
						"utf-8");
				//System.out.print("entityName:" + entityName);
			} catch (UnsupportedEncodingException e) {
				// TODO: handle exception
				entityName = "";
			}

			EntityDiagram diagram = diagramService
					.getDiagramByUri(Const.URI_DIAGRAM + ":" + uri);
			if (diagram != null) {
				String xml = "";
				if (diagram.getFilepath() != null) {
					xml = FileUtils.readFileToString(new File(Const.ABSOLUTE_DIAGRAM_PATH+diagram
							.getFilepath()));
					System.out.println("diagram path"+Const.ABSOLUTE_DIAGRAM_PATH+diagram
							.getFilepath());
					if (entityName != null && !entityName.equals("")) {
						DiagramXmlParser parser = new DiagramXmlParser();
						xml = parser.highLightEntity(xml, entityName);
					}
				}
				modelMap.put("xml", xml);
				modelMap.put("name", entityName);
				modelMap.put("success", "true");
			} else {
				modelMap.put("success", "false");
			}
			return modelMap;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			modelMap.put("success", "false");
		}
		return modelMap;
	}

	@RequestMapping(value = "/{uri}", method = RequestMethod.GET)
	public ModelAndView getDiagramByUri(
			@PathVariable(value = "uri") String uri,
			@RequestParam(value = "entityName", required = false) String entityName) {
		ModelAndView model = new ModelAndView();
		model.setViewName("diagram");
		try {
			try {
				// entityName = URLDecoder.decode(entityName,"UTF-8");
				entityName = new String(entityName.getBytes("ISO8859_1"),
						"utf-8");
				//System.out.print("entityName:" + entityName);
			} catch (UnsupportedEncodingException e) {
				// TODO: handle exception
				entityName = "";
			}
			EntityDiagram diagram = diagramService
					.getDiagramByUri(Const.URI_DIAGRAM + ":" + uri);
			if (diagram != null) {
				model.addObject("uri", uri);
				model.addObject("entityName", entityName);
			}
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}
}
